﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ProjectMadaktariMVC.Models
{
    public class DomainList
    {
        public enum DomainListEnum
        {
            Diagnosis,
            Hospitals,
            ProcedureTypes,
            referralLocations,
            ConcomitantDiagnosis
        }

        public DomainListEnum DomainListType { get; set; }

        public List<DomainListEnum> GetDomainListTypes()
        {
            return Enum.GetValues(typeof(DomainListEnum)).Cast<DomainListEnum>().ToList();
        }

        public Dictionary<int, string> GetDomainListValues(DomainListEnum listType)
        {
            using (MadaktariDataContext data = new MadaktariDataContext())
            {
                switch (listType)
                {
                    case DomainListEnum.ConcomitantDiagnosis:
                        return data.ConcomitantDiagnosis.ToDictionary(x => x.Id, y => y.Name);
                    case DomainListEnum.Diagnosis:
                        return data.Diagnosis.ToDictionary(x => x.Id, y => y.Name);
                    case DomainListEnum.Hospitals:
                        return data.Hospitals.ToDictionary(x => x.Id, y => y.Name);
                    case DomainListEnum.ProcedureTypes:
                        return data.ProcedureTypes.ToDictionary(x => x.Id, y => y.Name);
                    case DomainListEnum.referralLocations:
                        return data.ReferralLocations.ToDictionary(x => x.Id, y => y.Name);
                    default:
                        throw new Exception("Unknown Domain List type selected.");
                }
            }
        }

        public void Save(DomainListEnum listType, Dictionary<int, string> data)
        {
            using (MadaktariDataContext database = new MadaktariDataContext())
            {
                switch (listType)
                {
                    case DomainListEnum.ConcomitantDiagnosis:
                        foreach (var item in data)
                        {
                            var row = database.ConcomitantDiagnosis.FirstOrDefault(x => x.Id == item.Key);
                            if (row == null)
                                database.ConcomitantDiagnosis.InsertOnSubmit(
                                    new ConcomitantDiagnosi() { Id = item.Key, Name = item.Value });
                            else
                                row.Name = item.Value;
                        }
                        break;
                    case DomainListEnum.Diagnosis:
                        foreach (var item in data)
                        {
                            var row = database.Diagnosis.FirstOrDefault(x => x.Id == item.Key);
                            if (row == null)
                                database.Diagnosis.InsertOnSubmit(new Diagnosi() { Id = item.Key, Name = item.Value });
                            else
                                row.Name = item.Value;
                        }
                        break;
                    case DomainListEnum.Hospitals:
                        foreach (var item in data)
                        {
                            var row = database.Hospitals.FirstOrDefault(x => x.Id == item.Key);
                            if (row == null)
                                database.Hospitals.InsertOnSubmit(new Hospital() { Id = item.Key, Name = item.Value });
                            else
                                row.Name = item.Value;
                        }
                        break;
                    case DomainListEnum.ProcedureTypes:
                        foreach (var item in data)
                        {
                            var row = database.ProcedureTypes.FirstOrDefault(x => x.Id == item.Key);
                            if (row == null)
                                database.ProcedureTypes.InsertOnSubmit(new ProcedureType() { Id = item.Key, Name = item.Value });
                            else
                                row.Name = item.Value;
                        }
                        break;
                    case DomainListEnum.referralLocations:
                        foreach (var item in data)
                        {
                            var row = database.ReferralLocations.FirstOrDefault(x => x.Id == item.Key);
                            if (row == null)
                                database.ReferralLocations.InsertOnSubmit(new ReferralLocation() { Id = item.Key, Name = item.Value });
                            else
                                row.Name = item.Value;
                        }
                        break;
                    default:
                        throw new Exception("Unknown Domain List type selected.");
                }
                database.SubmitChanges();
            }
        }
    }
}